///
//  ImageClip v 1.0
//  Russell Lowke, Febrauary 28th 2011
//
//  Copyright (c) 2011 Lowke Media
//  see http://www.lowkemedia.com for more information
//
//  Permission is hereby granted, free of charge, to any person obtaining a 
//  copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the 
//  Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in 
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
//  IN THE SOFTWARE. 
//
//


///
// convert frames per second (FPS) to miliseconds per frame (MSPF)
//
ImageClip.fpsToMSPF = function(fps) 
{
    if (fps === 0 || isNaN(fps)) {
        return 0;   // avoid divide by zero
    } else {
        return (1000/fps);
    }
}
    
    
function ImageClip(images,                          // (array) array of images for this clip
                   fps,                             // (number) frames per second image list to be played at
                   playEveryFrame)                  // (boolean) if true no frames are skipped
{
    // public vars
    this.images = images;                           // (array) array of images for this clip
    this.frame = 0;                                 // (number) value of frame number (index)
    this.frameIndex = 0;                            // (uint) image index now displayed
    this.updateTime;                                // (uint) time of update
    this.timePassed;                                // (uint) time since last update
    this.playEveryFrame = playEveryFrame || false;  // (boolean) if true no frames are skipped
    if (this.images) {
        this.image = this.images[0];                // (image) set image to first image in images
    }
    
    // private vars
    var _fps = fps;                                 // (number) frames per second image list to be played at
    var _mspf = ImageClip.fpsToMSPF(_fps);          // (number) miliseconds per frame
    
    
    ///
    // update this ImageClip into canvas context
    //
    this.draw = function(ctx)                       // (context) canvas context being drawn into
    {    
        var updateTime = new Date().getTime();
        
        // take care of 1st draw
        if (this.updateTime === undefined) {
            this.updateTime = updateTime;
        }
        
        // calculate time passed
        this.timePassed = updateTime - this.updateTime;
        
        // record time of update
        this.updateTime = updateTime;
        
        // handle mspf
        if (_mspf) {
        
            // account for change
            var change = this.timePassed/_mspf;
            if (this.playEveryFrame && Math.abs(change) > 1) {
                if (change > 0) {
                    change = +1;
                } else {
                    change = -1;
                }
            }
            this.frame += change;
            
            // keep within bounds
            this.frame %= this.images.length;
            if (this.frame < 0) {
                this.frame += this.images.length;
            }
        }
        
        // find current frame
        if (this.images) {
            this.frameIndex = Math.floor(this.frame);
            this.image = this.images[this.frameIndex];
        }
        
        ImageClip.prototype.draw.call(this, ctx);
    }
    
    //
    // getters and setters
    
    this.setFPS = function(fps) {
        _fps = fps;
        _mspf = ImageClip.fpsToMSPF(_fps);
    }

    this.getFPS = function() {
        return _fps;
    }

}

ImageClip.prototype = new Bitmap();                 // ImageClip class extends Bitmap

